package ar.uade.distribuidas.tpo.dba.dao;

import java.util.List;

import org.hibernate.Session;



import ar.uade.distribuidas.tpo.dba.dominio.Rodamiento;
import ar.uade.distribuidas.tpo.dba.dominio.Stock;
import ar.uade.distribuidas.tpo.dba.hbt.HibernateDAO;



public class StockDAO extends HibernateDAO {

	public static StockDAO instance;
	
	private StockDAO() {}

	public static StockDAO getInstance() {
		if (instance == null){
			instance = new StockDAO();
		}
		return instance;
	}
	
	@SuppressWarnings("unchecked")
	public List<Stock> getStockFromRodamiento(Rodamiento rod) {
		
		List<Stock> st = (List<Stock>) this.getSession().createQuery("from Stock st where st.rodamiento.sufijo = :sufijo AND st.rodamiento.nroSerie = :nroSerie AND st.cantidad > 0")
				.setParameter("nroSerie", rod.getNroSerie())
				.setParameter("sufijo", rod.getSufijo())
				.list();		
		
		return st;
	
	}
	
public Stock getStockByRodamientoId(Long idRod) {
		
		@SuppressWarnings("unchecked")
		List<Stock> st = (List<Stock>) this.getSession().createQuery("from Stock st where st.rodamiento.id = :idRod")
				.setParameter("idRod", idRod)
				.list();
		
		if(st != null &&  !st.isEmpty())
			return st.get(0);
		
		return null;
	
	}
	
}